/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package VIEW;

import CONTROLLERS.SupplierController;
import UTIL.InputValidator;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author belthazod
 */
public class SupplierUI extends javax.swing.JPanel {
    static SupplierController supplierController;
            
    /**
     * Creates new form AddSupplier
     */
    public SupplierUI() {
        initComponents();
        supplierListTable.getColumnModel().getColumn(0).setMaxWidth(0);
        supplierListTable.getColumnModel().getColumn(0).setMinWidth(0);
        supplierListTable.getColumnModel().getColumn(0).setPreferredWidth(0);
        supplierController = new SupplierController(supplierListTable, 
                ProductsUI.getAddProductSupplierComboBox(), 
                ProductsUI.getEditProductSupplierComboBox(),
                ProductsUI.getProductFilterSupplierComboBox(),
                TransferUI.getTransferProductFilterSupplierComboBox(),
                DeliveryUI.getDeliveryProductFilterSupplierComboBox(), 
                InventoryUI.getProductFilterSupplierComboBox(),
                ReplacementUI.getSupplierComboBox(),
                Reports.getSupplierComboBox(), NewReturns.getSupplierComboBox(),
                AdjustmentsNew.getFilterSupplierComboBox());
        InputValidator.enabler(supplierListTable, editSupplierButton);
        InputValidator.enabler(supplierListTable, deleteSupplierButton);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editSupplierDialog = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        supplierNameEditInput = new javax.swing.JTextField();
        saveEditedSupplierButton = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        supplierIDEditInput = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        supplierNameInput = new javax.swing.JTextField();
        addSupplierButton = new javax.swing.JButton();
        deleteSupplierButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        supplierListTable = new javax.swing.JTable();
        editSupplierButton = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        editSupplierDialog.setTitle("Edit Supplier Information");
        editSupplierDialog.setBounds(new java.awt.Rectangle(0, 0, 400, 200));
        editSupplierDialog.setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

        jLabel3.setText("Supplier Name:");

        supplierNameEditInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supplierNameEditInputActionPerformed(evt);
            }
        });
        supplierNameEditInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                supplierNameEditInputKeyPressed(evt);
            }
        });

        saveEditedSupplierButton.setText("Save");
        saveEditedSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveEditedSupplierButtonActionPerformed(evt);
            }
        });

        jButton5.setText("Cancel");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        supplierIDEditInput.setText("jLabel6");

        javax.swing.GroupLayout editSupplierDialogLayout = new javax.swing.GroupLayout(editSupplierDialog.getContentPane());
        editSupplierDialog.getContentPane().setLayout(editSupplierDialogLayout);
        editSupplierDialogLayout.setHorizontalGroup(
            editSupplierDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(editSupplierDialogLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(saveEditedSupplierButton)
                .addGap(18, 18, 18)
                .addComponent(jButton5)
                .addGap(39, 39, 39))
            .addGroup(editSupplierDialogLayout.createSequentialGroup()
                .addGroup(editSupplierDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(editSupplierDialogLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(supplierIDEditInput))
                    .addGroup(editSupplierDialogLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addComponent(supplierNameEditInput, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );
        editSupplierDialogLayout.setVerticalGroup(
            editSupplierDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, editSupplierDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(supplierIDEditInput)
                .addGap(18, 18, 18)
                .addGroup(editSupplierDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(supplierNameEditInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(editSupplierDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(saveEditedSupplierButton))
                .addGap(21, 21, 21))
        );

        supplierIDEditInput.setVisible(false);

        editSupplierDialog.setLocationRelativeTo(this);

        setBackground(new java.awt.Color(204, 204, 204));
        setMaximumSize(new java.awt.Dimension(814, 592));
        setPreferredSize(new java.awt.Dimension(814, 592));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel1.setText("Supplier Name:");

        supplierNameInput.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        supplierNameInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                supplierNameInputActionPerformed(evt);
            }
        });
        supplierNameInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                supplierNameInputKeyPressed(evt);
            }
        });

        addSupplierButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        addSupplierButton.setText("Add Supplier");
        addSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSupplierButtonActionPerformed(evt);
            }
        });

        deleteSupplierButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        deleteSupplierButton.setText("Delete Supplier");
        deleteSupplierButton.setEnabled(false);
        deleteSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteSupplierButtonActionPerformed(evt);
            }
        });

        supplierListTable.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        supplierListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Supllier ID", "Supplier Name"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        supplierListTable.setRowHeight(20);
        supplierListTable.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                supplierListTableComponentAdded(evt);
            }
        });
        jScrollPane1.setViewportView(supplierListTable);
        if (supplierListTable.getColumnModel().getColumnCount() > 0) {
            supplierListTable.getColumnModel().getColumn(0).setResizable(false);
        }

        editSupplierButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        editSupplierButton.setText("Edit Supplier");
        editSupplierButton.setEnabled(false);
        editSupplierButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editSupplierButtonActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Supplier");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(editSupplierButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteSupplierButton))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(20, 20, 20)
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(supplierNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(10, 10, 10)
                            .addComponent(addSupplierButton))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 832, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(11, 39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel6)
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(supplierNameInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addSupplierButton))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editSupplierButton)
                    .addComponent(deleteSupplierButton))
                .addGap(73, 73, 73))
        );
    }// </editor-fold>//GEN-END:initComponents

        String host = "jdbc:mysql://localhost:3306/inventory";
        String uName = "root";
        String uPass = "";
    private void supplierListTableComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_supplierListTableComponentAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_supplierListTableComponentAdded
    private boolean checkInputsIfNull(JTextField input1, JTextField input2){
        if(input1.getText().equals("") || input2.getText().equals("")){
            return false;
        }
        else{
            return true;
        }
    }
    private void addSupplierButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSupplierButtonActionPerformed
        supplierController.addSupplier(supplierNameInput);
        supplierController.updateTableContents();
    }//GEN-LAST:event_addSupplierButtonActionPerformed

    private void editSupplierButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSupplierButtonActionPerformed
        Integer selectedRow = supplierListTable.getSelectedRow();
        String result = (String) supplierListTable.getModel().getValueAt(selectedRow, 0);
        JTextField[] inputs = {supplierNameEditInput};
        if(supplierController.openEditDialog(result, inputs, supplierIDEditInput)){
            editSupplierDialog.setVisible(true);
        }
    }//GEN-LAST:event_editSupplierButtonActionPerformed

    private void supplierNameEditInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supplierNameEditInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_supplierNameEditInputActionPerformed

    private void saveEditedSupplierButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveEditedSupplierButtonActionPerformed
        supplierController.editSupplier(supplierNameEditInput, supplierIDEditInput.getText());
        supplierController.updateTableContents();
        editSupplierDialog.dispose();
    }//GEN-LAST:event_saveEditedSupplierButtonActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        editSupplierDialog.dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void supplierNameInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_supplierNameInputActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_supplierNameInputActionPerformed

    private void deleteSupplierButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteSupplierButtonActionPerformed
        Integer selectedRow = supplierListTable.getSelectedRow();
        String name = (String) supplierListTable.getModel().getValueAt(selectedRow, 1);
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + name + " from the Supplier List?","Warning!" ,JOptionPane.YES_NO_OPTION);
        
        if(result == 0){
            supplierController.deleteSelectedSupplier();
            supplierController.updateTableContents();
        }
    }//GEN-LAST:event_deleteSupplierButtonActionPerformed

    private void supplierNameInputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_supplierNameInputKeyPressed
        if (evt.getKeyCode()== KeyEvent.VK_ENTER)
	{
            addSupplierButton.doClick();
	}
    }//GEN-LAST:event_supplierNameInputKeyPressed

    private void supplierNameEditInputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_supplierNameEditInputKeyPressed
        if (evt.getKeyCode()== KeyEvent.VK_ENTER)
	{
            saveEditedSupplierButton.doClick();
	}
    }//GEN-LAST:event_supplierNameEditInputKeyPressed
    public static void updateSupplierTable(){
        supplierController.updateTableContents();
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addSupplierButton;
    private javax.swing.JButton deleteSupplierButton;
    private javax.swing.JButton editSupplierButton;
    private javax.swing.JDialog editSupplierDialog;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton saveEditedSupplierButton;
    private javax.swing.JLabel supplierIDEditInput;
    private javax.swing.JTable supplierListTable;
    protected javax.swing.JTextField supplierNameEditInput;
    private javax.swing.JTextField supplierNameInput;
    // End of variables declaration//GEN-END:variables
}
